using Infrastructure.Attribute;
using ZR.Model.FacManage.Dto;
using ZR.Model.FacManage;
using ZR.Repository;
using ZR.Service.FacManage.IFacManageService;
using Infrastructure;
using Mapster;

namespace ZR.Service.FacManage
{
    /// <summary>
    /// 工厂信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IFacFactoryService), ServiceLifetime = LifeTime.Transient)]
    public class FacFactoryService : BaseService<FacFactory>, IFacFactoryService
    {
        /// <summary>
        /// 查询工厂信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<FacFactoryDto> GetPageList(FacFactoryQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<FacFactory, FacFactoryDto>(parm);

            return response;
        }

        /// <summary>
        /// 查询工厂信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<FacFactoryDto> GetList(FacFactoryQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToList().Adapt<List<FacFactoryDto>>();

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="FactoryId"></param>
        /// <returns></returns>
        public FacFactory GetInfo(string FactoryId)
        {
            var response = Queryable()
                .Where(x => x.FactoryId == FactoryId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加工厂信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public FacFactory AddFacFactory(FacFactory model)
        {
            if (this.Any(it => it.FactoryCode == model.FactoryCode))
            {
                throw new CustomException($"系统中已包含{model.FactoryCode}编码信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改工厂信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateFacFactory(FacFactory model)
        {
            if (this.Any(it => it.FactoryCode == model.FactoryCode && it.FactoryId != model.FactoryId))
            {
                throw new CustomException($"系统中已包含{model.FactoryCode}编码信息");
            }
            return Update(model, true);
        }

        /// <summary>
        /// 删除工厂信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteFacFactory(string[] ids)
        {
            if (this.Context.Queryable<FacWorkshop>().Any(it => ids.Contains(it.FactoryId)))
            {
                throw new CustomException($"系统中已经存在此工厂的车间信息，不能删除");
            }
            return Delete(ids);
        }

        /// <summary>
        /// 导出工厂信息
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<FacFactoryDto> ExportList(FacFactoryQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .Select((it) => new FacFactoryDto()
                {
                }, true)
                .ToPage(parm);

            return response;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<FacFactory> QueryExp(FacFactoryQueryDto parm)
        {
            var predicate = Expressionable.Create<FacFactory>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.FactoryCode), it => it.FactoryCode == parm.FactoryCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.FactoryName), it => it.FactoryName.Contains(parm.FactoryName));
            return predicate;
        }
    }
}